const app = getApp()
const util = require('../../utils/util.js')

Page({
  data: {
    loading: 1,
    currentTab: 0
  },

  onLoad: function (options) {
    console.log(options)
    let identity = app.globalData.identityId
 
    this.setData({
      identity: identity,
      common: app.globalData.features.indexOf("35") != -1 ? 0 : 1
    })

    if (options && options.fromSta && this.data.common == 0) {
      this.setData({
        currentTab: 1
      })
    }
    
    if (this.data.common == 0) {
      this.setData({
        windowHeight: app.globalData.windowHeight * 0.94 - 160,
        windowHeightMa: app.globalData.windowHeight * 0.94 - 40,
      })
      this.data.currentTab ? this.getSignInRecord() : this.getMeetingList()
    } else {
      // 没有添加签到权限，只有签到权限
      this.setData({
        windowHeight: app.globalData.windowHeight - 240
      })
      this.getSignInRecord()
    }
  },


  qrScan: util.throttle(function () {
    util.scanQRCode()
  }),

  // 查询已创建的会议
  getMeetingList() {
    wx.request({
      url: app.kaoqinUrl + '/WeChat/AttendanceSys/getMeetingInfo',
      method: 'POST',
      data: {
        manager: app.globalData.userId
      },
      dataType: 'json',
      success: res => {
        this.setData({
          meetingListMa: res.data
        })
      },
      fail: res => {
        wx.showToast({
          title: '网络异常！',
          image: '/images/warning.png'
        })
      },
      complete: () => {
        this.setData({
          loading: 0
        })
      }
    })
  },

  // 查询所有已签到的会议
  getSignInRecord() {
    if ("-2" == app.globalData.identityId){
      //学生获取上课
      wx.request({
        url: app.kaoqinUrl + '/WeChat/AttendanceSys/getUserCheckIn',
        method: 'POST',
        data: {
          manager: app.globalData.userId
        },
        dataType: 'json',
        success: res => {
          if (res.data.data) {
            this.setData({
              meetingList: res.data.data
            })
          }
        },
        fail: res => {
          wx.showToast({
            title: '网络异常！',
            image: '/images/warning.png'
          })
        },
        complete: () => {
          this.setData({
            loading: 0
          })
        }
      })
    }else{
      //教师获取会议
      wx.request({
        url: app.kaoqinUrl + '/WeChat/AttendanceSys/getTeacherCheckIn',
        method: 'POST',
        data: {
          name: app.globalData.realname
        },
        dataType: 'json',
        success: res => {
          if (res.data) {
            this.setData({
              meetingList: res.data
            })
          }
        },
        fail: res => {
          wx.showToast({
            title: '网络异常！',
            image: '/images/warning.png'
          })
        },
        complete: () => {
          this.setData({
            loading: 0
          })
        }
      })
    }
    
  },

  toOneMeeting(e) {
    console.log(e.currentTarget.dataset)
    wx.navigateTo({
      url: `/pages/signInSetting/signInSetting?meetingId=${e.currentTarget.dataset.meetingid}&type=${e.currentTarget.dataset.type}&name=${e.currentTarget.dataset.name}`,
    })
  },

  existMeeting(e) {
    wx.setStorageSync("signInQr", this.data.meetingList[e.currentTarget.dataset.index])
    wx.navigateTo({
      url: '/pages/signInRegister/signInRegister?type=from exist',
    })
  },

  bindChange: util.throttle(function (e) {
    console.log(e)
    this.setData({
      loading: 1,
      currentTab: e.detail.current
    })
    if (this.data.currentTab == 0) {
      this.getMeetingList()
    } else {
      this.getSignInRecord()
    }
  }, 300),

  swichNav: function (e) {
    this.setData({
      loading: 1,
      currentTab: e.currentTarget.dataset.current
    })
    if (this.data.currentTab == 0) {
      this.getMeetingList()
    } else {
      this.getSignInRecord()
    }
  },
})
